$(function () {
    // 采购申请信息
    vm.purchaseApplySearch();

    // 获取耗材分类树信息
    $.get(baseURL + "material/materialcategory/getTree/", function(r){
        vm.materialCategoryList = r.materialCategoryTree;
        vm.e.materialUnitEnum = r.materialUnitEnum;
        vm.e.materialSupplier = r.materialSupplier;
        vm.e.purchaseApplyStatusEnum = r.purchaseApplyStatusEnum;
        vm.e.selectMDList = r.getSelectMD;

        // console.log( vm.e.materialSupplier);
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        page:{
            current:1, // 当前页
            size:10, // 分页数
            total:0, // 总条数
        },
        applyPage:{
            current:1, // 当前页
            size:15, // 分页数
            total:0, // 总条数
            tableData:[], //数据列
        },
        applyTableHeight:  window.innerHeight - 50, // table高度
        q: {
            applyNo: null,
            sellerId: null,
            status: null,
        },
        e: {
            materialUnitEnum : [],
            purchaseApplyStatusEnum: [],
			selectMDList: [],
            materialSupplier: [], /*采购供应商*/
            taxEnum:[{value: 1,label: '含税'},{value: 0,label: '不含税'}],
        },
        materialCategorySearch: '',
        materialCategoryList: [],
        defaultProps: {
            children: 'children',
            label: 'label'
        },
        materialTableData: [],
        purchaseApplyForm: {
            storePurchaseApplyEntity:{
                totalMoney: 0.00,
                tax: null,
                remark: null,
            },
            storePurchaseApplyDetailEntityList: []
        },
        materialSearchForm: {
            materialName: null
        },
        addMaterialList: [] // 已添加耗材ID集合
    },
    watch: {
        materialCategorySearch(val) {
            this.$refs.tree2.filter(val);
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.storePurchaseApply = {};
            vm.purchaseApplyForm = {
                storePurchaseApplyEntity:{
                    totalMoney: 0.00,
                    tax: 1,
                    remark: null,
                },
                storePurchaseApplyDetailEntityList: []
            }
            vm.addMaterialList = [];
            vm.materialTableData = [];
            console.log(vm.e.taxEnum)
        },
        onSubmit: function (event) {
            $.ajax({
                type: "POST",
                url: baseURL + "purchase/storepurchaseapply/save",
                contentType: "application/json",
                data: JSON.stringify(this.purchaseApplyForm),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getInfo: function(applyId){
            $.get(baseURL + "purchase/storepurchaseapply/info/"+applyId, function(r){
                vm.storePurchaseApply = r.storePurchaseApply;
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.purchaseApplySearch();
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        /*查询耗材*/
        materialsSearch(data) {
            var  paramData = {}
            if(typeof(data.id) == "undefined"){
                paramData.materialName =  this.materialSearchForm.materialName;
            }else{
                paramData.categoryId = data.id;
                this.materialSearchForm.materialName = null;
            }
            var queryData = {page: this.page, params: paramData};
            //ajax 请求
            $.ajax({
                type: "POST",
                url: baseURL + "material/material/queryMaterialsList",
                contentType: "application/json",
                data: JSON.stringify(queryData),
                success: function (r) {
                    if (r.code == 0) {
                        vm.page.size = r.page.size;
                        vm.page.total = r.page.total;
                        vm.materialTableData = r.page.records;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        /*分页数改变*/
        handleSizeChange(val) {
            vm.page.size = val;
            this.materialsSearch(val);
        },
        /*分页当前页改变改变*/
        handleCurrentChange(val) {
            this.page.current = val
            this.materialsSearch(val);
        },
        /*格式化 单位*/
        formatUnit: function (row, column, cellValue, index) {
            for(var i=0; i<this.e.materialUnitEnum.length; i++){
                if(cellValue == this.e.materialUnitEnum[i].id){
                    return this.e.materialUnitEnum[i].describe
                }
            }
        },

		/*格式化 含税*/
		formatTax: function (row, column, cellValue, index) {
			for(var i=0; i<this.e.taxEnum.length; i++){
				if(cellValue == this.e.taxEnum[i].value){
					return this.e.taxEnum[i].label
				}
			}
		},
        // 格式化 采购申请状态
        formatStatus: function (row, column, cellValue, index) {
            for(var i=0; i<this.e.purchaseApplyStatusEnum.length; i++){
                if(cellValue == this.e.purchaseApplyStatusEnum[i].id){
                    return this.e.purchaseApplyStatusEnum[i].describe
                }
            }
        },

        // 格式化 要选择的门店
        formatMD: function (row, column, cellValue, index) {
			for(var i=0; i<this.e.selectMDList.length; i++){
				if(cellValue == this.e.selectMDList[i].sellerId){
					return this.e.selectMDList[i].sellerName
				}
			}
		},

        /*添加耗材到采购列表*/
        addMaterial: function (row) {
            if(this.addMaterialList.indexOf(row.materialId)>=0){
                alert("耗材："+row.materialName+" 已添加！")
                return;
            }
            var storePurchaseApplyDetail = {
                materialId:row.materialId,
                materialName:row.materialName,
                specifications:row.specifications,
                length:row.length,
                width:row.width,
                supplierId:row.supplierId,
                unit:row.unit,
                materialQuantity:1,
                totalMoney:0,
                weight:row.weight
            }
            this.purchaseApplyForm.storePurchaseApplyDetailEntityList.push(storePurchaseApplyDetail);
            this.addMaterialList.push(row.materialId);
        },
        /*移除采购列表耗材*/
        removePurchaseMaterials: function (row,index) {
            this.purchaseApplyForm.storePurchaseApplyDetailEntityList.splice(index,1)

            var materialIdIndex = vm.addMaterialList.indexOf(row.materialId);
            vm.addMaterialList.splice(materialIdIndex,1);

            vm.purchaseApplyForm.storePurchaseApplyEntity.totalMoney = Number(vm.purchaseApplyForm.storePurchaseApplyEntity.totalMoney)
                - Number(row.totalMoney)
        },
        /*单个耗材，采购总价修改事件*/
        totalMoneyChange: function (row,index) {
            var storePurchaseApplyDetail = this.purchaseApplyForm.storePurchaseApplyDetailEntityList[index];
            storePurchaseApplyDetail.unitMoney = percentNum(storePurchaseApplyDetail.totalMoney,storePurchaseApplyDetail.materialQuantity)
            this.purchaseApplyForm.storePurchaseApplyDetailEntityList.splice(index,1,storePurchaseApplyDetail);

            // 修改采购总价
            this.changePurchaseTotal();
        },
        // 修改采购总价
        changePurchaseTotal: function () {
            vm.purchaseApplyForm.storePurchaseApplyEntity.totalMoney = 0
            for(var i=0; i<this.purchaseApplyForm.storePurchaseApplyDetailEntityList.length; i++){
                vm.purchaseApplyForm.storePurchaseApplyEntity.totalMoney = Number(vm.purchaseApplyForm.storePurchaseApplyEntity.totalMoney)
                    + Number(vm.purchaseApplyForm.storePurchaseApplyDetailEntityList[i].totalMoney);
            }
        },
        /*分页数改变*/
        applySizeChange(val) {
            this.applyPage.size = val;
            this.purchaseApplySearch();
        },
        /*分页当前页改变改变*/
        applyCurrentChange(val) {
            this.applyPage.current = val
            this.purchaseApplySearch();
        },
        // 采购申请查询
        purchaseApplySearch: function () {
            var queryData = {page: this.applyPage, params: this.q};
            //ajax 请求
            $.ajax({
                type: "POST",
                url: baseURL + "purchase/storepurchaseapply/querylistPage",
                contentType: "application/json",
                data: JSON.stringify(queryData),
                success: function (r) {
                    if (r.code == 0) {
                        vm.applyPage.size = r.page.size;
                        vm.applyPage.total = r.page.total;
                        vm.applyPage.tableData = r.page.records;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 采购申请审核
        audit: function (row) {
            window.storepurchaseapplyAuditVm.loadData({
                'materialUnitEnum':this.e.materialUnitEnum,
                'storePurchaseApply': row,
                'materialSupplier':this.e.materialSupplier
            });
        },
        //查询条件重置
        reset:function(){
            vm.q.applyNo = null;
            vm.q.status = null;
            vm.q.sellerId = null;
        }
    }
});

function percentNum(num,num2) {
    return (Math.round(num / num2 * 100) / 100.00);
}